/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

import BO.Controller;
import BO.Postit;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Wase
 */
@ManagedBean
@SessionScoped
public class ManagerBean {

    private Controller controller;
    private String userEmail;
    private Integer friendId;
    private String postIt;
    private HttpSession session;
    
    public String getPostIt() {
        return postIt;
    }

    public void setPostIt(String postIt) {
        this.postIt = postIt;
    }

    public Integer getFriendId() {
        return friendId;
    }

    public void setFriendId(Integer friendEmail) {
        this.friendId = friendEmail;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }
    
    public String makeFriend(Integer userId, Integer friendId) {
        String status = controller.makeFriend(userId, friendId);
        return status;
    }
    
    public String removeFriend(Integer userId, Integer friendId) {
        String status = controller.removeFriend(userId, friendId);
        return status;
    }
    
    public String submitPostIt(Integer userId) {
        String status = controller.submitPostIt(userId, postIt);
        return status;
    }
    
    public List<Postit> getPostits() {
        UserView user = (UserView) session.getAttribute("user");
        List<Postit> postits = controller.getLatestPostitsByUserId(user.getId());
        return postits;
    }
    
    public String stalkOrUnstalkFriend(String id) {
        UserView user = (UserView) session.getAttribute("user");
        String status = controller.makeFriend(user.getId(), Integer.parseInt(id));
        if (status.equals("befriended")) {
            return status;
        } else if (status.equals("alreadyFriended")) {
            status = controller.removeFriend(user.getId(), Integer.parseInt(id));
        }
        return "";
    }
    
    public String stalkable(Integer id) {
        UserView user = (UserView) session.getAttribute("user");
        String status = controller.stalkable(user.getId(), id);
        if (!status.equals("befriendable")) {
            return "Unstalk";
        }
        return "Stalk!";
    }
    
    public List<UserView> getFriends() {
        UserView user = (UserView) session.getAttribute("user");
        List<UserView> friends = controller.getFriendsByUserId(user.getId());
        return friends;
    }
    
    /** Creates a new instance of ManagerBean */
    public ManagerBean() {
        controller = new Controller();
        session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    }
}
